package com.wlyuan.open.scanner.domain.combine;

import com.wlyuan.open.scanner.dao.entity.OrderIndexDO;
import com.wlyuan.open.scanner.domain.transport.mapping.CacheCostMapping;
import com.wlyuan.open.scanner.domain.transport.mapping.CloseMapping;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import com.wlyuan.open.utils.LongUtils;
import lombok.var;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Optional;

/**
 * @author lijing
 * @description
 */
public class CombineConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = MappingInvalid.class)
    public interface Convert {

        @Mappings({
                @Mapping(target = "id", source = "indexId"),
                @Mapping(target = "sourceKey", source = "indexId"),
                @Mapping(target = "createTime", source = "indexCreatetime"),
                @Mapping(target = "tenantId", source = "indexCreatorcompanyid"),

                @Mapping(target = "transportIds", source = "childrenTransportIds"),
                @Mapping(target = "orderNo", source = "indexCode"),
                @Mapping(target = "contractNo", source = "indexPactcode"),
                @Mapping(target = "fromTime", source = "indexFromtime"),
                @Mapping(target = "toTime", source = "indexTotime"),
                @Mapping(target = "dispatchBy", source = "dispatchBy"),
                @Mapping(target = "dispatchAt", source = "dispatchAt"),
                @Mapping(target = "acceptBy", source = "indexConfirmer"),
                @Mapping(target = "acceptAt", source = "indexConfirmtime"),
                @Mapping(target = "status", source = "combineStatus"),
                @Mapping(target = "createBy", source = "indexCreator"),
                @Mapping(target = "createAt", source = "indexCreatetime"),
                @Mapping(target = "updateAt", source = "indexStatustime"),
                @Mapping(target = "desc", source = "indexDescription"),

                @Mapping(target = "customerId", source = "indexCustomerid"),
                @Mapping(target = "customerName", source = "customerName"),
                @Mapping(target = "customerType", source = "customerType"),
                @Mapping(target = "customerTenantId", source = "downTenantId"),

                @Mapping(target = "carrierId", source = "carrierId"),
                @Mapping(target = "carrierType", source = "carrierType"),
                @Mapping(target = "carrierName", source = "carrierName"),
                @Mapping(target = "carrierPhone", source = "carrierPhone"),
                @Mapping(target = "carrierContactName", source = "carrierContactName"),
                @Mapping(target = "carrierTenantId", source = "upTenantId"),

                @Mapping(target = "trackType", source = "indexTracktype"),
                @Mapping(target = "deviceCode", source = "indexDevicecode"),
                @Mapping(target = "type", source = "transportType"),
        })
        @BaseMapping
        @CacheCostMapping
        @CloseMapping
        Combine convert(OrderIndexDO source);

        @AfterMapping
        default void after(@MappingTarget Combine target, OrderIndexDO source) {
            target.setRaw(JsonUtils.withNullJson(source));
        }

        List<Combine> convert(List<OrderIndexDO> list);

    }

    public static List<Combine> convert(List<OrderIndexDO> creation) {
        return CONVERT.convert(creation);
    }

    public static Combine convert(OrderIndexDO source) {
        var convert = CONVERT.convert(source);
        Optional.ofNullable(convert).ifPresent(c -> {
            if (source.isQuotation()) {
                c.setCost(null);
            }
            //无关单
            if (c.getClose() != null && !LongUtils.notNullLong(c.getClose().getFlowId())) {
                c.setClose(null);
            }
        });
        return convert;
    }

}
